﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{
    public enum METype
    {
        Turbine =0,//汽轮机
        Generator=1,//发电机
    }

    public class Cls_Project
    {
        private XmlDocument _xmlDoc = null;
        private XmlNode _XmlNode = null;
        public string _strFileName = "";
        private XmlDocument _xmlOriginDoc = null;

        public string FileName
        {
            get
            {
                return _strFileName;
            }
        }

        public Cls_ACQUnits ACQUnitList
        {
            get
            {
                XmlNode x = _XmlNode.SelectSingleNode("ACQUnitList");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ACQUnitList"));
                   // return null;
                return new Cls_ACQUnits(x);
            }
        }

        public Cls_Speeds SpeedList 
        {
            get 
            {
                XmlNode x = _XmlNode.SelectSingleNode("SpeedList");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("SpeedList"));
                // return null;
                return new Cls_Speeds(x);
            }
        }




        public Cls_ProjectTable ProjectTable
        {
            get
            {
                XmlNode x = _XmlNode.SelectSingleNode("ProjectTable");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ProjectTable"));
                    //return null;
                return new Cls_ProjectTable(x);
            }
        }

        public int TypeId
        {
            get
            {
                if (_XmlNode["TypeId"] == null || _XmlNode["TypeId"].InnerText=="")
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(_XmlNode["TypeId"].InnerText);
                }
               
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["TypeId"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("TypeId"));
                _XmlNode["TypeId"].InnerText = value.ToString();
            }
        }

        public string Company
        {
            get
            {
                return _XmlNode["Company"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["Company"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("Company"));
                _XmlNode["Company"].InnerText = value.ToString();
            }
        }

        public string METype
        {
            get
            {
                return _XmlNode["METype"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["METype"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("METype"));
                _XmlNode["METype"].InnerText = value.ToString();
            }
        }

        public string ShipName
        {
            get
            {
                return _XmlNode["ShipName"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ShipName"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ShipName"));
                _XmlNode["ShipName"].InnerText = value.ToString();
            }
        }

        public string SerialNumber 
        {
            get
            {
                return _XmlNode["SerialNumber"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["SerialNumber"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("SerialNumber"));
                _XmlNode["SerialNumber"].InnerText = value.ToString();
            }
        }

        public string TesterName
        {
            get
            {
                return _XmlNode["TesterName"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["TesterName"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("TesterName"));
                _XmlNode["TesterName"].InnerText = value.ToString();
            }
        }

        public string HostMac
        {
            get
            {
                return _XmlNode["HostMac"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["HostMac"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("HostMac"));
                _XmlNode["HostMac"].InnerText = value.ToString();
            }
        }

        public string HostIPAddress
        {
            get
            {
                return _XmlNode["HostIPAddress"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["HostIPAddress"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("HostIPAddress"));
                _XmlNode["HostIPAddress"].InnerText = value.ToString();
            }
        } 

        public DateTime DateOfManufacture
        {
            get
            {
                return DateTime.Parse(_XmlNode["DateOfManufacture"].InnerText);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["DateOfManufacture"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("DateOfManufacture"));
                _XmlNode["DateOfManufacture"].InnerText = value.ToString();
            }
        }

        public DateTime InstallationDate
        {
            get
            {
                return DateTime.Parse(_XmlNode["InstallationDate"].InnerText);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["InstallationDate"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("InstallationDate"));
                _XmlNode["InstallationDate"].InnerText = value.ToString();
            }
        }

        

        //create xml documents
        public void Create()
        {
            _xmlDoc = new XmlDocument();
            _xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");

            XmlNode x = _xmlDoc.AppendChild(_xmlDoc.CreateElement("Project"));
            _XmlNode = x;

          //  x.AppendChild(_xmlDoc.CreateElement("Company"));

            x.AppendChild(_xmlDoc.CreateElement("ACQUnitList"));

            _xmlOriginDoc = new XmlDocument();
            _xmlOriginDoc.CreateXmlDeclaration("1.0", "UTF-8", "");
            _xmlOriginDoc.LoadXml(_xmlDoc.OuterXml);
        }

        public bool Open(string strFileName)
        {
            bool bRet = true;
            try
            {
                _xmlDoc = new XmlDocument();
                _xmlDoc.Load(strFileName);
                _XmlNode = _xmlDoc.SelectSingleNode("Project");
                _xmlOriginDoc = new XmlDocument();
                _xmlOriginDoc.LoadXml(_xmlDoc.OuterXml);

            }
            catch (System.Exception)
            {
                bRet = false;
            }

            _strFileName = strFileName;
            return bRet;
        }

        public bool IsChanged()
        {
            //compare two files
            try
            {
                return _xmlDoc.OuterXml.Trim() != _xmlOriginDoc.OuterXml.Trim();
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Save()
        {
            //this.Tasks.Sort((x, y) => x.id.CompareTo(y.id));
            return SaveAs(_strFileName);
        }

        public bool SaveAs(string name)
        {
            // this.Tasks.Sort((x, y) => x.id.CompareTo(y.id));

            _xmlDoc.Save(name);
            _strFileName = name;

            //reset old text
            _xmlOriginDoc.LoadXml(_xmlDoc.OuterXml);
            return true;
        }
    }
}
